@echo off
title AI Agent Tool

echo ================================================
echo Builder.Alyr.Net AI Agent Detection Tool
echo (c) 2020-2025 ALYR.NET
echo ================================================
echo.

cd /d "%~dp0"

echo Starting AI Agent Detection System...
echo.

echo Checking project environment...
if not exist "package.json" (
    echo ERROR: package.json file not found!
    echo Please run this script in project root directory
    pause
    exit /b 1
)

if not exist "utils\ai-bootstrap.js" (
    echo ERROR: AI Agent core file not found!
    echo Please check if utils\ai-bootstrap.js exists
    pause
    exit /b 1
)

echo Project environment check passed
echo.

echo Checking dependencies...

rem Check if node_modules exists
if not exist "node_modules" (
    echo node_modules directory not found, installing all dependencies...
    npm install
    if errorlevel 1 (
        echo ERROR: Failed to install dependencies
        pause
        exit /b 1
    )
    echo All dependencies installed successfully
    goto :dependency_check_done
)

rem Check core dependencies
set MISSING_DEPS=0

if not exist "node_modules\fs-extra" (
    echo Missing dependency: fs-extra
    set MISSING_DEPS=1
)

if not exist "node_modules\prisma" (
    echo Missing dependency: prisma
    set MISSING_DEPS=1
)

if not exist "node_modules\@nestjs" (
    echo Missing dependency: @nestjs/cli
    set MISSING_DEPS=1
)

if not exist "node_modules\@types\express" (
    echo Missing dependency: @types/express
    set MISSING_DEPS=1
)

if not exist "node_modules\@types\node" (
    echo Missing dependency: @types/node
    set MISSING_DEPS=1
)

if not exist "node_modules\@types\react" (
    echo Missing dependency: @types/react
    set MISSING_DEPS=1
)

if not exist "node_modules\@types\react-dom" (
    echo Missing dependency: @types/react-dom
    set MISSING_DEPS=1
)

if %MISSING_DEPS%==1 (
    echo.
    echo Installing missing dependencies...
    npm run setup
    if errorlevel 1 (
        echo Setup failed, trying npm install...
        npm install
        if errorlevel 1 (
            echo ERROR: Failed to install dependencies
            pause
            exit /b 1
        )
    )
    echo All dependencies installed successfully
) else (
    echo All dependencies check passed
)

:dependency_check_done

echo.
echo Launching AI Agent Detection Tool...
echo.

rem Ensure we are in the correct directory
cd /d "%~dp0"

node utils\ai-bootstrap.js

if errorlevel 1 (
    echo.
    echo ERROR: AI Agent startup failed!
    echo Please check:
    echo 1. Node.js is installed
    echo 2. Run npm install
    echo 3. Run as administrator
    pause
    exit /b 1
)

echo.
echo AI Agent detection completed successfully!
echo.
pause